/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aljfce.business.search.control;

import com.aljfce.business.entity.Calendar;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.sql.DataSource;
import com.aljfce.business.entity.User;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author EOroan
 */
@Stateless
public class SearchManager {

    public SearchManager() {
    }
    
    
    @PersistenceContext(unitName = "meteocalPU")
    private EntityManager em;
    
    private User user;

    /**
     * Get the value of user
     *
     * @return the value of user
     */
    public User getUser() {
        return user;
    }

    /**
     * Set the value of user
     *
     * @param user new value of user
     */
    public void setUser(User user) {
        this.user = user;
    }


    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")

    //TODO

    /**
     *
     * @param email of the user to search
     * @return a User entity found from the database
     */
        public User findUser(String email) {
        return em.find(User.class, email);
    }

    public void loadUser(User user) {
        if (user == null){
            System.out.println("\n No User Found!!!");
        }else{
            this.user = user;   
        }
    }

    public void persist(Object object) {
        try {
            em.persist(object);
        } catch (Exception e) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", e);
            throw new RuntimeException(e);
        }
    }

    public Calendar getCalendar() {
        List<Calendar> Calendar = (List<Calendar>)user.getCalendarCollection();
        int i = 0;
        while(i < Calendar.size() || !(Calendar.get(i).getUser().equals(user)))
            i++;
        //when the calendar is the one searched!!
        return Calendar.get(i);
    }
    
    
}
